package com.taikang.hmp.hbusiness.diabetes.measure.meal.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taikang.hmp.hbusiness.diabetes.measure.meal.bean.PagodaBean;

/** 
 * 膳食
 * author:qrp
 * time:2015年8月11日 下午3:45:22
 */
public class MealUtil {
	/**
	 * 根据所输入的参数取得建议摄入量
	 * @param sex  性别
	 * @param height  身高
	 * @param weight  体重
	 * @return 建议摄入量 （单位：千卡）
	 */
	public static int getRecommendedIntake(String sex,float height,float weight,int age){
		double result = 0;
		if(sex.equals("0")){
			result = 1.2*(66 + 13.7 * weight+ 5 * height - 6.8*age);
		}else{
			result = 1.2*(655 + 9.6 * weight+ 1.8 * height - 4.7*age);
		}
		return (int)result;
	}
	
	/**
	 * 取得能量级别
	 * @param intake 建议摄入量
	 * @return 能量级别 （单位：千卡）
	 */
	public static int getEnergyLevel(int intake){
		int result = 0;
		if(intake < 1400){
			result = 1200;
		}else if(intake >= 1400 && intake <1600){
			result = 1400;
		}else if(intake >= 1600 && intake <1800){
			result = 1600;
		}else if(intake >= 1800 && intake <2000){
			result = 1800;
		}else if(intake >= 2000 && intake <2200){
			result = 2000;
		}else if(intake >= 2200 && intake <2400){
			result = 2200;
		}else if(intake >= 2400){
			result = 2400;
		}
		return result;
	}
	/**
	 * 判断新用户水果膳食处方
	 * @param parm1 空腹血糖 (单位：mmol/L)
	 * @param parm2 糖化血红蛋白
	 * @return 0：含水果;1：不含水果
	 */
	public static String getNewUserFruitResult(float parm1,float parm2){
		String result = "0";
		if(parm1 > 7.0){
			result = "1";
		}else if(parm1 <= 7.0 && parm2 <= 7.5){
			result = "0";
		}else if(parm1 <= 7.0 && parm2 > 7.5){
			result = "1";
		}else if(parm1 > 7.0 && parm2 > 7.5){
			result = "1";
		}else if(parm1 > 7.0 && parm2 <= 7.5){
			result = "0";
		}
		return result;
	}
	/**
	 * 判断注册后一个月后水果膳食处方
	 * @param parm1 空腹血糖 (单位：mmol/L)
	 * @param parm2 餐后两小时血糖 (单位：mmol/L)
	 * @return 0：含水果;1：不含水果
	 */
	public static String getRegisterUserFruitResult(List<String> parm1,List<String> parm2){
		String result = "";
		int count1 = 0;
		float avg1 = 0;
		float avg2 = 0;
		if(parm1!=null && parm1.size()>0){
		for(int i=0;i<parm1.size();i++){
			if(Float.parseFloat(parm1.get(i)) <= 7.0){
				count1++;
			}
		}
		 avg1 = count1/parm1.size();
		}
		int count2 = 0;
		if(parm2!=null && parm2.size()>0){
		for(int j=0;j<parm2.size();j++){
			if(Float.parseFloat(parm2.get(j)) <= 10.0){
				count2++;
			}
		}
		 avg2 = count2/parm2.size();
		}
		if(avg1 > 0.8 && avg2 > 0.8 ){
			result = "0";
		}else{
			result = "1";
		}
		return result;
	}
	/**
	 * 通过某天获取当月第一天和最后天的日期 (格式 xxxx-xx-xx)
	 * @param date xxxx-xx-xx
	 * @return
	 */
	public static Map<String, String> getfirstendDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, date.getYear());
		cal.set(Calendar.MONTH, date.getMonth());
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		Date lastDate = cal.getTime();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		Date firstDate = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, String> result = new HashMap<String, String>();
		result.put("first", format.format(firstDate));
		result.put("last", format.format(lastDate));
		return result;
	}
	/**
	 * 通过生日获取年龄
	 * @param birthday
	 * @return
	 */
	public static String getAgeByBirthday(String param) throws Exception{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date birthDay = format.parse(param);
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
					"The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		}
		return age + "";
	}
	/**
	 * 初始化宝塔
	 * @return
	 */
	public static List<PagodaBean> initPagoda(){
		List<PagodaBean> prescription = new ArrayList<PagodaBean>();
		//含水果
		PagodaBean pagoda1 = new PagodaBean();
		pagoda1.setStaple("150");
		pagoda1.setVegetable("300");
		pagoda1.setFruit("100");
		pagoda1.setEgg("50");
		pagoda1.setSeafood("40");
		pagoda1.setMeat("30");
		pagoda1.setNut("15");
		pagoda1.setMilk("150");
		pagoda1.setOil("20");
		pagoda1.setSalt("6");
		pagoda1.setIs_has_fruit(0);
		pagoda1.setEnergyLevel(1200);
		prescription.add(pagoda1);
		PagodaBean pagoda2 = new PagodaBean();
		pagoda2.setStaple("200");
		pagoda2.setVegetable("300");
		pagoda2.setFruit("100");
		pagoda2.setEgg("50");
		pagoda2.setSeafood("40");
		pagoda2.setMeat("30");
		pagoda2.setNut("15");
		pagoda2.setMilk("150");
		pagoda2.setOil("20");
		pagoda2.setSalt("6");
		pagoda2.setIs_has_fruit(0);
		pagoda2.setEnergyLevel(1400);
		prescription.add(pagoda2);
		PagodaBean pagoda3 = new PagodaBean();
		pagoda3.setStaple("240");
		pagoda3.setVegetable("400");
		pagoda3.setFruit("100");
		pagoda3.setEgg("50");
		pagoda3.setSeafood("40");
		pagoda3.setMeat("35");
		pagoda3.setNut("15");
		pagoda3.setMilk("180");
		pagoda3.setOil("20");
		pagoda3.setSalt("6");
		pagoda3.setIs_has_fruit(0);
		pagoda3.setEnergyLevel(1600);
		prescription.add(pagoda3);
		PagodaBean pagoda4 = new PagodaBean();
		pagoda4.setStaple("270");
		pagoda4.setVegetable("500");
		pagoda4.setFruit("150");
		pagoda4.setEgg("50");
		pagoda4.setSeafood("60");
		pagoda4.setMeat("40");
		pagoda4.setNut("20");
		pagoda4.setMilk("200");
		pagoda4.setOil("20");
		pagoda4.setSalt("6");
		pagoda4.setIs_has_fruit(0);
		pagoda4.setEnergyLevel(1800);
		prescription.add(pagoda4);
		PagodaBean pagoda5 = new PagodaBean();
		pagoda5.setStaple("300");
		pagoda5.setVegetable("500");
		pagoda5.setFruit("200");
		pagoda5.setEgg("50");
		pagoda5.setSeafood("70");
		pagoda5.setMeat("45");
		pagoda5.setNut("25");
		pagoda5.setMilk("250");
		pagoda5.setOil("22");
		pagoda5.setSalt("6");
		pagoda5.setIs_has_fruit(0);
		pagoda5.setEnergyLevel(2000);
		prescription.add(pagoda5);
		PagodaBean pagoda6 = new PagodaBean();
		pagoda6.setStaple("330");
		pagoda6.setVegetable("500");
		pagoda6.setFruit("200");
		pagoda6.setEgg("50");
		pagoda6.setSeafood("80");
		pagoda6.setMeat("50");
		pagoda6.setNut("25");
		pagoda6.setMilk("270");
		pagoda6.setOil("23");
		pagoda6.setSalt("6");
		pagoda6.setIs_has_fruit(0);
		pagoda6.setEnergyLevel(2200);
		prescription.add(pagoda6);
		PagodaBean pagoda7 = new PagodaBean();
		pagoda7.setStaple("360");
		pagoda7.setVegetable("500");
		pagoda7.setFruit("200");
		pagoda7.setEgg("50");
		pagoda7.setSeafood("90");
		pagoda7.setMeat("55");
		pagoda7.setNut("30");
		pagoda7.setMilk("300");
		pagoda7.setOil("25");
		pagoda7.setSalt("6");
		pagoda7.setIs_has_fruit(0);
		pagoda7.setEnergyLevel(2400);
		prescription.add(pagoda7);
		//不含水果
		PagodaBean pagoda8 = new PagodaBean();
		pagoda8.setStaple("150");
		pagoda8.setVegetable("500");
		pagoda8.setFruit("0");
		pagoda8.setEgg("50");
		pagoda8.setSeafood("40");
		pagoda8.setMeat("30");
		pagoda8.setNut("15");
		pagoda8.setMilk("150");
		pagoda8.setOil("20");
		pagoda8.setSalt("6");
		pagoda8.setIs_has_fruit(1);
		pagoda8.setEnergyLevel(1200);
		prescription.add(pagoda8);
		PagodaBean pagoda9 = new PagodaBean();
		pagoda9.setStaple("200");
		pagoda9.setVegetable("500");
		pagoda9.setFruit("0");
		pagoda9.setEgg("50");
		pagoda9.setSeafood("40");
		pagoda9.setMeat("30");
		pagoda9.setNut("15");
		pagoda9.setMilk("150");
		pagoda9.setOil("20");
		pagoda9.setSalt("6");
		pagoda9.setIs_has_fruit(1);
		pagoda9.setEnergyLevel(1400);
		prescription.add(pagoda9);
		PagodaBean pagoda10 = new PagodaBean();
		pagoda10.setStaple("250");
		pagoda10.setVegetable("500");
		pagoda10.setFruit("0");
		pagoda10.setEgg("50");
		pagoda10.setSeafood("60");
		pagoda10.setMeat("40");
		pagoda10.setNut("20");
		pagoda10.setMilk("2000");
		pagoda10.setOil("20");
		pagoda10.setSalt("6");
		pagoda10.setIs_has_fruit(1);
		pagoda10.setEnergyLevel(1600);
		prescription.add(pagoda10);
		PagodaBean pagoda11 = new PagodaBean();
		pagoda11.setStaple("290");
		pagoda11.setVegetable("500");
		pagoda11.setFruit("0");
		pagoda11.setEgg("50");
		pagoda11.setSeafood("60");
		pagoda11.setMeat("40");
		pagoda11.setNut("20");
		pagoda11.setMilk("200");
		pagoda11.setOil("20");
		pagoda11.setSalt("6");
		pagoda11.setIs_has_fruit(1);
		pagoda11.setEnergyLevel(1800);
		prescription.add(pagoda11);
		PagodaBean pagoda12 = new PagodaBean();
		pagoda12.setStaple("325");
		pagoda12.setVegetable("500");
		pagoda12.setFruit("0");
		pagoda12.setEgg("50");
		pagoda12.setSeafood("70");
		pagoda12.setMeat("45");
		pagoda12.setNut("25");
		pagoda12.setMilk("250");
		pagoda12.setOil("22");
		pagoda12.setSalt("6");
		pagoda12.setIs_has_fruit(1);
		pagoda12.setEnergyLevel(2000);
		prescription.add(pagoda12);
		PagodaBean pagoda13 = new PagodaBean();
		pagoda13.setStaple("350");
		pagoda13.setVegetable("500");
		pagoda13.setFruit("0");
		pagoda13.setEgg("50");
		pagoda13.setSeafood("80");
		pagoda13.setMeat("50");
		pagoda13.setNut("25");
		pagoda13.setMilk("270");
		pagoda13.setOil("23");
		pagoda13.setSalt("6");
		pagoda13.setIs_has_fruit(1);
		pagoda13.setEnergyLevel(2200);
		prescription.add(pagoda13);
		PagodaBean pagoda14 = new PagodaBean();
		pagoda14.setStaple("380");
		pagoda14.setVegetable("500");
		pagoda14.setFruit("0");
		pagoda14.setEgg("50");
		pagoda14.setSeafood("90");
		pagoda14.setMeat("55");
		pagoda14.setNut("30");
		pagoda14.setMilk("300");
		pagoda14.setOil("25");
		pagoda14.setSalt("6");
		pagoda14.setIs_has_fruit(1);
		pagoda14.setEnergyLevel(2400);
		prescription.add(pagoda14);
		return prescription;
	}

}
